package hmis.pos.Controller;

import hmis.pos.Common.pojo.ApiResponse;
import hmis.pos.Common.vo.ReportForm.FinalRoomStatistics;
import hmis.pos.Common.vo.ReportForm.RoomStatistics;
import hmis.pos.Common.vo.SearchPrice.RoomPriceTotal;
import hmis.pos.Common.vo.SearchPrice.TypeAndDate;
import hmis.pos.Entity.RoomPrice;
import hmis.pos.Service.RoomPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("roomPrice")
@CrossOrigin(origins={"http://localhost:3000"})
public class RoomPriceController {

    @Autowired
    private RoomPriceService roomPriceService;

    @RequestMapping("save")
    @ResponseBody
    public RoomPrice save(@RequestBody RoomPrice roomPrice){
        return roomPriceService.save(roomPrice);
    }

    @RequestMapping("delete")
    @ResponseBody
    public boolean delete(@RequestBody RoomPrice roomPrice){
        return roomPriceService.delete(roomPrice);
    }

    @RequestMapping("update")
    @ResponseBody
    public RoomPrice update(@RequestBody RoomPrice roomPrice){
        return roomPriceService.update(roomPrice);
    }

    @RequestMapping("findById")
    @ResponseBody
    public RoomPrice findById(@RequestParam String id){
        return roomPriceService.findById(id);
    }

    @RequestMapping("findAll")
    @ResponseBody
    public List<RoomPrice> findAll(){
        return roomPriceService.findAll();
    }

    @RequestMapping("findByType")
    @ResponseBody
    public List<RoomPrice> findByType(@RequestParam String type){
        return roomPriceService.findByType(type);
    }

    @RequestMapping("findByDate")
    @ResponseBody
    public List<RoomPrice> findByDate(@RequestParam String dateString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date utilDate = sdf.parse(dateString);
        Date date = new java.sql.Date(utilDate.getTime());
        return roomPriceService.findByDate(date);
    }

    @RequestMapping("findByTypeAndDate")
    @ResponseBody
    public List<RoomPrice> findByTypeAndDate(@RequestBody TypeAndDate typeAndDate) {
        return roomPriceService.findByTypeAndDate2(typeAndDate);
    }

    @RequestMapping("findRoomPrice")
    @ResponseBody
    public List<RoomPriceTotal> findRoomPrice(@RequestBody TypeAndDate typeAndDate){
        return roomPriceService.findByTypeAndDate(typeAndDate);
    }

    @RequestMapping("setDayPrice")
    @ResponseBody
    public ApiResponse<RoomPrice> setDayPrice(@RequestBody RoomPrice roomPrice) throws ParseException {
        return roomPriceService.setDayPrice(roomPrice);
    }

    @RequestMapping("setMonthPrice")
    @ResponseBody
    public ApiResponse<RoomPrice> setMonthPrice(@RequestBody RoomPrice roomPrice) throws ParseException {
        return roomPriceService.setMonthPrice(roomPrice);
    }

    @RequestMapping("findByMonth")
    @ResponseBody
    public List<RoomPrice> findByMonth(@RequestParam String dateString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date utilDate = sdf.parse(dateString);
        Date date = new java.util.Date(utilDate.getTime());
        return roomPriceService.findByMonth(date);
    }


    @RequestMapping("getYearData")
    @ResponseBody
    public FinalRoomStatistics getYearData(@RequestParam int thisYear){
        return roomPriceService.getYearData(thisYear);
    }

    @RequestMapping("getMonthData")
    @ResponseBody
    public FinalRoomStatistics getMonthData(@RequestParam int thisYear){
        return roomPriceService.getMonthData(thisYear);
    }

    @RequestMapping("getSeasonData")
    @ResponseBody
    public FinalRoomStatistics getSeasonData(@RequestParam int thisYear){
        return roomPriceService.getSeasonData(thisYear);
    }
}
